package si2.robo;

import jason.environment.grid.GridWorldModel;
import jason.environment.grid.Location;

import java.util.ArrayList;

import elements.Element;
import elements.GarbageGroup;

public class AspiradorModel extends GridWorldModel {
		
	public boolean INICIALIZACAO = true; // para envio das perc. iniciais
	
	private int idGarbageGroup;
	
	private ArrayList<Aspirador> aspiradores;
	
	public AspiradorModel(int sizeHGrid, int sizeVGrid, int numberOfAgents, ArrayList<Aspirador> aspiradores) {		
		super(sizeHGrid, sizeVGrid, numberOfAgents); 		
		
		idGarbageGroup = 0;		
		
		this.aspiradores = aspiradores;
		
		try {
			for(Aspirador asp : aspiradores) {
				setAgPos(asp.getAgentNumber(), asp.getX(), asp.getY()); 
				asp.setModel(this);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<Aspirador> getAspiradores() {
		return aspiradores;
	}

	public void adicionarLixo(){
		
		if (Math.random()*100 < 20) {
			//System.out.println("ENTREI NA TAXA");
			int garbageGroupX = (int)(Math.random()*AspiradorEnvironment.TAMHGrid);
			int garbageGroupY = (int)(Math.random()*AspiradorEnvironment.TAMVGrid);
			
			if (verifyGarbagePosition(garbageGroupX, garbageGroupY)) {
				//System.out.println("PASSEI NO VERIFY: " + garbageGroupX + " " + garbageGroupY);
				
				GarbageGroup gg = new GarbageGroup(idGarbageGroup, garbageGroupX, garbageGroupY, this);
				AspiradorEnvironment.garbageGroupsList.add(gg);
				idGarbageGroup++;
			} else {
				//System.out.println("FALHEI NO VERIFY: " + garbageGroupX + " " + garbageGroupY);
			}
		}		
	}
	
	private boolean positionOccuppied(int x, int y){
		for(int i = 0; i < AspiradorEnvironment.garbageList.size(); i++){
			Element element = AspiradorEnvironment.garbageList.get(i);
			if(element.getX() == x && element.getY() == y){
				return true;
			}
		}
		return false;
	}
	
	
	
	public boolean verifyGarbagePosition(int x, int y) {
		for (int i = 0; i < AspiradorEnvironment.garbageGroupsList.size(); i++) {
			GarbageGroup currentGarbageGroup = AspiradorEnvironment.garbageGroupsList.get(i);
			
			if (currentGarbageGroup.getX() == x && currentGarbageGroup.getY() == y) {
				return false;
			}
			
			if (currentGarbageGroup.getX() == x-1 && currentGarbageGroup.getY() == y-1) {
				return false;
			}
			if (currentGarbageGroup.getX() == x-1 && currentGarbageGroup.getY() == y) {
				return false;
			}
			if (currentGarbageGroup.getX() == x-1 && currentGarbageGroup.getY() == y+1) {
				return false;
			}
			
			if (currentGarbageGroup.getX() == x && currentGarbageGroup.getY() == y-1) {
				return false;
			}
			if (currentGarbageGroup.getX() == x && currentGarbageGroup.getY() == y+1) {
				return false;
			}
			
			if (currentGarbageGroup.getX() == x+1 && currentGarbageGroup.getY() == y-1) {
				return false;
			}
			if (currentGarbageGroup.getX() == x+1 && currentGarbageGroup.getY() == y) {
				return false;
			}
			if (currentGarbageGroup.getX() == x+1 && currentGarbageGroup.getY() == y+1) {
				return false;
			}

		}
		
		for (int i = 0; i < aspiradores.size(); i++) {
			Location l = getAgPos(i);
			
			if (l.x == x && l.y == y) {
				return false;
			}
			if (l.x == x+1 && l.y == y) {
				return false;
			}
			if (l.x == x && l.y == y+1) {
				return false;
			}
			if (l.x == x+1 && l.y == y+1) {
				return false;
			}
		}
		
		if (x == AspiradorEnvironment.TAMHGrid-1 || y == AspiradorEnvironment.TAMVGrid-1) {
			return false;
		}
		
		return true;
	}
	

}
